define(["jquery", "easy-admin", "xm-select"], function ($, ea) {
    let laydate = layui.laydate
        , form = layui.form;
    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'coupon.coupon/index',
        add_url: 'coupon.coupon/add',
        edit_url: 'coupon.coupon/edit',
        delete_url: 'coupon.coupon/delete',
        export_url: 'coupon.coupon/export',
        modify_url: 'coupon.coupon/modify',
        push_url: 'coupon.coupon/push'
    };

    var Controller = {

        index: function () {
            ea.table.render({
                init: init,
                toolbar: ['refresh',
                    [{
                        text: '添加',
                        url: init.add_url,
                        method: 'open',
                        auth: 'add',
                        class: 'layui-btn layui-btn-normal layui-btn-sm',
                        icon: 'fa fa-plus ',
                        extend: 'data-full="false"',
                    }],
                    'delete', 'export'],
                minWidth: 90,
                cols: [[
                    {type: 'checkbox'},
                    {field: 'id', title: '砍价id', width: 90},
                    {field: 'title', title: '优惠券名称', minWidth: 140},
                    {field: 'face_val', title: '面值', minWidth: 90,search: false},
                    {field: 'use_limit', title: '使用条件 > 元', minWidth: 120},
                    {field: 'surplus_count', title: '剩余数量', minWidth: 120,search: false},
                    {field: 'use_desc', title: '使用说明', minWidth: 120,search: false},
                    {field: 'receive_limit', title: '每人限领', minWidth: 120,search: false},
                    {field: 'receive_day_limit', title: '人/日限领', minWidth: 120,search: false},
                    {field: 'start_time', title: '开始时间', minWidth: 180,templet:ea.table.date},
                    {field: 'end_time', title: '结束时间', minWidth: 180,templet: ea.table.date},
                    {field: 'push', title: '是否发布', minWidth: 120, search: 'select', selectList: ['未推送', '已推送']},
                    {field: 'new_limit', title: '新用户领取', minWidth: 120, templet: ea.table.switch, tips: '是|否'},
                    {
                        field: 'status',
                        search: 'select',
                        selectList: ["禁用", "启用"],
                        title: '状态',
                        templet: ea.table.switch,
                        width: 180
                    },
                    {field: 'create_time', title: '创建时间', width: 160, search: 'range'},
                    {
                        width: 250, title: '操作', templet: ea.table.tool, operat: [
                            [{
                                text: '推送',
                                extra: 'name',
                                url: init.push_url,
                                method: 'request',
                                auth: 'push',
                                class: 'layui-btn layui-btn-xs layui-btn-normal',
                                extend: '',
                                show: [
                                    {"name": "push", "value": "0"}
                                ]
                            }, {
                                text: '编辑',
                                extra: 'name',
                                url: init.edit_url,
                                method: 'open',
                                auth: 'edit',
                                class: 'layui-btn layui-btn-xs layui-btn-success',
                                extend: 'data-full="true"',
                                show: [
                                    {"name": "push", "value": "0"}
                                ]
                            }],
                            'delete']
                    },
                ]],
            });

            ea.listen();
        },
        add: function () {
            form.on('radio(selectTypeList)', function (data) {
                if (data.value == 1) {
                    $(".money").slideDown();
                } else {
                    $(".money").slideUp();
                }
                return false;
            })
            form.on('radio(selectType)', function (data) {
                if (data.value == 1) {
                    $(".type_date").slideDown();
                    $(".type_day").slideUp();
                } else {
                    $(".type_date").slideUp();
                    $(".type_day").slideDown();
                }
                return false;
            })
            form.on('select(selectUseType)', function (data) {
                if (data.value == 2) {
                    $(".goods_list").slideDown();
                } else {
                    $(".goods_list").slideUp();
                }
            })
            //选择商品
            let goods_list = xmSelect.render({
                el: '#goods',
                toolbar: {
                    show: true,
                },
                name: 'goods_list',
                filterable: true,
                paging: true,
                pageSize: 10,
                pageEmptyShow: false,
                data: [],
                model: {
                    label: {
                        type: 'block',
                        block: {
                            //最大显示数量, 0:不限制
                            showCount: 5,
                            //是否显示删除图标
                            showIcon: true,
                        }
                    }
                },
            });
            $.get("/admin/mall.goods/getGoodsByListInfo").then(res => {
                goods_list.update({
                    data: res.data
                })
            })
            laydate.render({
                elem: '#start_time',
                type: 'datetime'
                , calendar: true
                , done: function (value, date, endDate) {

                    laydate.render({
                        elem: '#end_time',
                        type: 'datetime'
                        , calendar: true
                        , min: value
                        , done: function (val) {
                            laydate.render({
                                elem: '#use_end_time',
                                type: 'datetime'
                                , calendar: true
                                , min: val
                            })
                        }
                    })
                }
            })

            ea.listen();
        },
        edit: function () {
            form.on('radio(selectTypeList)', function (data) {
                if (data.value == 1) {
                    $(".money").slideDown();
                } else {
                    $(".money").slideUp();
                }
                return false;
            })
            form.on('radio(selectType)', function (data) {
                if (data.value == 1) {
                    $(".type_date").slideDown();
                    $(".type_day").slideUp();
                } else {
                    $(".type_date").slideUp();
                    $(".type_day").slideDown();
                }
                return false;
            })
            form.on('select(selectUseType)', function (data) {
                if (data.value == 2) {
                    $(".goods_list").slideDown();
                } else {
                    $(".goods_list").slideUp();
                }
            })
            //选择商品
            let goods_list = xmSelect.render({
                el: '#goods',
                toolbar: {
                    show: true,
                },
                name: 'goods_list',
                filterable: true,
                paging: true,
                pageSize: 10,
                pageEmptyShow: false,
                data: [],
                model: {
                    label: {
                        type: 'block',
                        block: {
                            //最大显示数量, 0:不限制
                            showCount: 5,
                            //是否显示删除图标
                            showIcon: true,
                        }
                    }
                },
            });
            $.get("/admin/mall.goods/getGoodsByListInfo").then(res => {
                goods_list.update({
                    data: res.data,
                })
            })
            let arr_pay = [];
            arr_pay = goods_lists.split(',');
            goods_list.update({
                initValue: arr_pay
            })
            laydate.render({
                elem: '#start_time',
                type: 'datetime'
                , calendar: true
                , done: function (value, date, endDate) {

                    laydate.render({
                        elem: '#end_time',
                        type: 'datetime'
                        , calendar: true
                        , min: value
                        , done: function (val) {
                            laydate.render({
                                elem: '#use_end_time',
                                type: 'datetime'
                                , calendar: true
                                , min: val
                            })
                        }
                    })
                }
            })
            laydate.render({
                elem: '#end_time',
                type: 'datetime'
                , calendar: true
                , min: h_start_time
                , done: function (val) {

                }
            })
            laydate.render({
                elem: '#use_end_time',
                type: 'datetime'
                , calendar: true
                , min: h_end_time
            })
            ea.listen();
        },
    };
    return Controller;
});